package com.huiquan.vocab.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.vocab.domain.VocabDyadPre;
import com.huiquan.vocab.domain.VocabDyadStd;
import com.huiquan.vocab.domain.VocabDyadStdOrder;
import com.huiquan.vocab.domain.VocabSymptomElementStd;
import com.huiquan.vocab.domain.VocabSymptomPOMerge;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class VocabDyadInitDao extends EnhancedBaseDao<VocabDyadStd> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "VOCAB_DYAD_INIT.";

	public VocabDyadInitDao() {
		super(space);
	}

	public void batchInsertPre(List<VocabDyadPre> list) {
		int start = 0, end, size = list.size();
		List<VocabDyadPre> tempList;

		while (start < size) {
			if (start + 2000 < size) {
				end = start + 2000;
			} else {
				end = size;
			}
			tempList = list.subList(start, end);
			this.insert(space + "BATCH_INSERT_PRE", tempList, sqlMapClient);
			start += 2000;
		}
	}

	public void updatePreOrderVocabAndCnt() {
		this.update(space + "UPDATE_PRE_ORDER_VOCAB_AND_CNT", sqlMapClient);
	}

	public void initPre2() {
		this.update(space + "INIT_PRE2", sqlMapClient);
	}

	public void initPre3() {
		this.update(space + "INIT_PRE3", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<VocabDyadPre> retrieveRemovePreList(Map<String, Object> param) {
		return (List<VocabDyadPre>) this.list(space + "RETRIEVE_REMOVE_PRE_LIST", param, sqlMapClient);
	}

	public void batchInsertPre3(List<VocabDyadPre> list) {
		this.insert(space + "BATCH_INSERT_PRE3", list, sqlMapClient);
	}

	public void setSynonymToPre3(Map<String, Object> param) {
		this.update(space + "SET_SYNONYM_TO_PRE3", param, sqlMapClient);
	}

	public void insertDyadByPre3() {
		this.update(space + "INSERT_DYAD_BY_PRE3", sqlMapClient);
	}

	public void deleteWrongPre3() {
		this.delete(space + "DELETE_WRONG_PRE3", sqlMapClient);
	}

	public void initRemove() {
		this.update(space + "INIT_REMOVE", sqlMapClient);
	}

	public void initRemoveStd(Long removeId) {
		Map<String, Object> param = new HashMap<>();
		param.put("removeId", removeId);

		this.update(space + "INIT_REMOVE_STD", param, sqlMapClient);
	}

	public void deleteRemovePre3() {
		this.delete(space + "DELETE_REMOVE_PRE3", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<VocabDyadStdOrder> retrieveStdOrder() {
		return (List<VocabDyadStdOrder>) this.list(space + "RETRIEVE_STD_ORDER", sqlMapClient);
	}

	public void batchInsertPage(List<VocabDyadStdOrder> list) {
		this.insert(space + "BATCH_INSERT_PAGE", list, sqlMapClient);
	}

	public void initOSSearch() {
		this.update(space + "INIT_O_S_SEARCH", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<VocabDyadPre> retrieveDeList(String type) {
		if (StringUtils.equals(type, "o+de")) {
			return (List<VocabDyadPre>) this.list(space + "RETRIEVE_O_DE_LIST", sqlMapClient);
		} else if (StringUtils.equals(type, "s+de")) {
			return (List<VocabDyadPre>) this.list(space + "RETRIEVE_S_DE_LIST", sqlMapClient);
		} else if (StringUtils.equals(type, "si+de")) {
			return (List<VocabDyadPre>) this.list(space + "RETRIEVE_SI_DE_LIST", sqlMapClient);
		}
		return new ArrayList<VocabDyadPre>();
	}

	public void initSymptomElemenStd() {

		this.update(space + "INIT_SYMPTOM_ELEMENT_STD", sqlMapClient);
	}

	public void initPOMerge() {

		this.update(space + "INIT_P_O_MERGE", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<VocabSymptomPOMerge> retrieveSymptomPOMerge() {

		return (List<VocabSymptomPOMerge>) this.list(space + "RETRIEVE_SYMPTOM_P_O_MERGE", sqlMapClient);
	}

	public void batchInsertSymptomElementStd(List<VocabSymptomElementStd> mergeResult) {
		int start = 0, end, size = mergeResult.size();
		List<VocabSymptomElementStd> tempList;

		while (start < size) {
			if (start + 2000 < size) {
				end = start + 2000;
			} else {
				end = size;
			}
			tempList = mergeResult.subList(start, end);
			this.insert(space + "BATCH_INSERT_SYMPTOM_ELEMENT_STD", tempList, sqlMapClient);
			start += 2000;
		}
	}

	public void deleteSymptomElementStdBySymptomIds(String ids) {

		this.delete(space + "DELETE_SYMPTOM_ELEMENT_STD_BY_SYMPTOM_IDS", ids, sqlMapClient);
	}

	public void initPreByElementStd() {

		this.update(space + "INIT_PRE_BY_ELEMENT_STD", sqlMapClient);
	}

	public void initDyadSentence() {

		this.update(space + "INIT_DYAD_SENTENCE", sqlMapClient);
	}

	public void batchInsertDyadSynonymyReference(List<Map<String, String>> list) {
		int start = 0, end, size = list.size();
		List<Map<String, String>> tempList;

		while (start < size) {
			if (start + 500 < size) {
				end = start + 500;
			} else {
				end = size;
			}
			tempList = list.subList(start, end);
			this.insert(space + "BATCH_INSERT_SYNONYMY_REFERENCE", tempList, sqlMapClient);
			start += 500;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrievePOMerge() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_PO_MERGE", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveOdeCombine() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_O_DE_DYAD_COMBINE", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveDyadSynonymy() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_DYAD_SYNONYMY", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveOOCombine() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_O_O_DYAD_COMBINE", sqlMapClient);
	}

	public void batchInsertInitPre(List<VocabDyadPre> list) {
		int start = 0, end, size = list.size();
		List<VocabDyadPre> tempList;

		while (start < size) {
			if (start + 500 < size) {
				end = start + 500;
			} else {
				end = size;
			}
			tempList = list.subList(start, end);
			this.insert(space + "BATCH_INSERT_INIT_PRE", tempList, sqlMapClient);
			start += 500;
		}
	}
}
